package test;

import java.io.*;

public class jShell {

	Thread tOut;// handle output of child process

	public jShell(String shellCommand) {

		Process child = null; // child process
		try {
			child = Runtime.getRuntime().exec(shellCommand);
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (child == null) {
			return;
		}

		final InputStream inputStream = child.getInputStream();
		final BufferedReader brOut = new BufferedReader(new InputStreamReader(
				inputStream));

		tOut = new Thread() { // initialize thread tOut
			String line;
			int lineNumber = 0;

			public void run() {
				try {
					while ((line = brOut.readLine()) != null) {
						System.out.println(lineNumber + ". " + line);
						lineNumber++;
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		};

	}

	public void startOut() { // start thread tOut
		if (tOut != null) {
			tOut.start();
		}
	}

	public void interruptOut() { // interrupt thread tOut
		if (tOut != null) {
			tOut.interrupt();
		}
	}

	public static void main(String[] args) {
		jShell shell = new jShell("java");
		shell.startOut();
	}

}
